package com.tuanzi.blog.web;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tuanzi.blog.model.Blog;
import com.tuanzi.blog.model.BlogWithBLOBs;
import com.tuanzi.blog.model.Type;
import com.tuanzi.blog.service.BlogService;
import com.tuanzi.blog.service.TypeService;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.List;

/**
 * 雄关漫道真如铁，而今迈过从头越
 **/

@Controller
public class TypeShowController {

    @Resource
    private TypeService typeService;

    @Resource
    private BlogService blogService;

    @GetMapping("/types/{id}")
    public String types(@PageableDefault(page = 1,size = 6,sort = {"updateTime"},direction = Sort.Direction.DESC) Pageable pageable,
                        Model model,
                        @PathVariable("id") Long id){
        //拿到所有的分类
        List<Type> allTypes = typeService.getAllTypes();
        if (id == -1){
            id = allTypes.get(0).getId();
        }
        model.addAttribute("types",allTypes);
        PageHelper.startPage(pageable.getPageNumber(), pageable.getPageSize() );
        List<BlogWithBLOBs> list = blogService.getTypeBlogs(id);
        PageInfo<BlogWithBLOBs> pageInfo = new PageInfo<>(list);
        model.addAttribute("page",pageInfo);
        model.addAttribute("activeTypeId",id);

        return "types";
    }

}
